import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, FormInstance } from "element-plus";
import {
  GetAllHaveSignedContractPurchaseOrdersName,
  ProductShipmentOrdersGetAllByPagedAndSort,
  ProductShipmentOrdersUpdateWhetherShip,
} from "@/api/stu/SupplyChainFinance";
import { addres } from "@/utils/public/addres"
import { FXQ } from "@/utils/public/FXQ"
import { emitter } from "@/utils/mitt";
import { useRoute } from "vue-router";
import { ref, reactive, nextTick, watch, onMounted, computed } from "vue";
import qs from "qs";
export function useSupplierMaterial() {
  const route = useRoute();
  const Vdialog = ref(false);
  const orderType = ref(false)
  const selectId = ref()
  const loading = ref(false);
  const dataList = ref([]);
  const FormDisabled = ref(true);
  const orderList = ref()
  const isSeal = ref(false) //第一个盖章
  const isSeal1 = ref(false)
  const nextStep = ref(false)
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    pageSizes: [10, 20, 50, 100],
    background: true
  });
  const columns = [
    {
      label: "序号",
      type: "index",
      minWidth: 50
    }, {
      label: "购销项目名称",
      prop: "purchaseName",
    }, {
      label: "托运单位",
      prop: "consigner",
    }, {
      label: "发货单编号",
      prop: "trackingNumber",
    },
    {
      label: "收件人地址",
      prop: "deliveryAddress",
    }, {
      label: "操作",
      fixed: "right",
      width: 140,
      slot: "operation"
    },
  ];
  const DispatchForm = reactive({
    trackingNumber: '',
    shipDate: '',
    adress: '',
    purchaseName: "",
    purchaseOrderId: '',
    receiveName: "",
    deliverName: "",
    carrierName: "****物流公司",
    orderDate: "",
    deliveryTime: "",
    infoList: '',
    originInfoList: '',
    id: '',
    totalMoney: 0,
    totalAmountInWords: ''
  });


  function MaterialDetails(row) {
    selectId.value = row.shipmentId
    orderType.value = row.whetherShip
    DispatchForm.totalAmountInWords = row.totalAmountInWords
    DispatchForm.shipDate = row.shippedDate.substring(0, 10)
    DispatchForm.trackingNumber = row.trackingNumber
    DispatchForm.adress = row.deliveryAddress
    DispatchForm.receiveName = row.consignee
    DispatchForm.deliverName = row.consigner
    DispatchForm.orderDate = row.orderDate.substring(0, 10)
    DispatchForm.deliveryTime = row.deliveryDate.substring(0, 10)
    DispatchForm.originInfoList = row.productInformations
    DispatchForm.infoList = JSON.parse(row.productInformations)
    DispatchForm.purchaseName = row.purchaseName
    DispatchForm.purchaseOrderId = row.purchaseOrderId
    let total = 0
    //计算商品总额
    DispatchForm.infoList.forEach(item => {
      item.total = item.Price * item.Quantity
      total = total + (item.total)
    })
    DispatchForm.totalMoney = total
    Vdialog.value = true
    nextTick(() => {
      const MYFXQ = FXQ.companySeal(row.consigner, 0, 0, '', '');
      let view_FQX = document.querySelector("#view_FQX");
      let MYFQX = `<img src='${MYFXQ}' style="width:100%;height:100%"  />`
      view_FQX.innerHTML = MYFQX;
      if (row.whetherShip) {
        Signet()
      }
    })

  }

  function handleSizeChange() {
    getList()
  }
  function handleCurrentChange() {
    getList()
  }
  async function getList() {
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
    }
    const res = await ProductShipmentOrdersGetAllByPagedAndSort(data);
    if (res.success) {
      dataList.value = res.data.items;
      pagination.total = res.data.totalCount
    } else {
      ElMessage.error(res.msg)
    }
  }
  async function getOrderList() {
    const data = qs.stringify({
      taskId: route.query.taskId,
      planId: route.query.planId,
      sEId: '',
    })
    const res = await GetAllHaveSignedContractPurchaseOrdersName(data);
    if (res.success) {
      orderList.value = res.data
    } else {
      ElMessage.error(res.msg)
    }
  }

  function Signet() {
    // // 生成章
    const MYFXQ = FXQ.companySeal(DispatchForm.carrierName, 0, 0, '', '');
    let view_FQX = document.querySelector("#view_FQXSHOW");
    let MYFQX = `<img src='${MYFXQ}' style="width:100%;height:100%"/>`
    view_FQX.innerHTML = MYFQX;
    isSeal.value = true
  }
  async function finish() {
    const params = qs.stringify({
      id: selectId.value
    })
    const data = {
      purchaseOrderId: DispatchForm.purchaseOrderId,
      outGoodsInformation: DispatchForm.originInfoList,
      totalAmountInWords: DispatchForm.totalAmountInWords,
      taskId: route.query.taskId,
      planId: route.query.planId,
    }
    let res = await ProductShipmentOrdersUpdateWhetherShip(params, data)
    if (res.success) {
      ElMessage.success('受理发货成功！');
      ClosePanelVisible()
      getList()
    } else {
      ElMessage.error(res.msg);
    }
  }
  function ClosePanelVisible() {
    DispatchForm.receiveName = ''
    DispatchForm.deliverName = ''
    DispatchForm.orderDate = ''
    DispatchForm.deliveryTime = ''
    DispatchForm.originInfoList = ''
    DispatchForm.infoList = ''
    DispatchForm.totalMoney = ''
    DispatchForm.purchaseName = ''
    DispatchForm.shipDate = ''
    Vdialog.value = false
    nextStep.value = false
    isSeal.value = false
    isSeal1.value = false
  }

  function clickNextStep() {
    if (!isSeal.value) {
      ElMessage.warning('请先完成盖章后进行下一步')
    } else {
      nextStep.value = true
    }
  }
  onMounted(() => {
    getList()
    getOrderList()
  })
  return {
    Vdialog,
    loading,
    columns,
    dataList,
    DispatchForm,
    FormDisabled,
    MaterialDetails,
    Signet,
    isSeal,
    nextStep,
    pagination,
    handleSizeChange,
    handleCurrentChange,
    orderList,
    clickNextStep,
    finish,
    ClosePanelVisible,
    orderType,
  }

}
